home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / util / moni / Scout-src.lha / source / objects / scout_locks.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-02-13  |  14.0 KB  |  411 lines

  1. /**
  2.  * Scout - The Amiga System Monitor
  3.  *
  4.  *------------------------------------------------------------------
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * You must not use this source code to gain profit of any kind!
  21.  *
  22.  *------------------------------------------------------------------
  23.  *
  24.  * @author Andreas Gelhausen
  25.  * @author Richard Körber <rkoerber@gmx.de>
  26.  */
  27.  
  28.  
  29.  
  30. #include "system_headers.h"
  31.  
  32. static APTR LockPool = NULL;
  33.  
  34. int lockanzahl, hiddenanzahl;
  35.  
  36. __asm __saveds LONG locklist_cmppathfunc (register __a1 struct LockEntry *le1, register __a2 struct LockEntry *le2)
  37. {
  38.    LONG cmp;
  39.  
  40.    cmp = strcmpi (le1->lock_path, le2->lock_path);
  41.    if (cmp == 0) cmp = strcmpi(le1->lock_address, le2->lock_address);
  42.  
  43.    return cmp;
  44. }
  45.  
  46. struct Hook locklist_cmppathhook = {
  47.  {NULL, NULL},
  48.  (ULONG (* )())locklist_cmppathfunc,
  49.  NULL, NULL
  50. };
  51.  
  52. __asm __saveds LONG locklist_dspfunc(register __a2 char **array, register __a1 struct LockEntry *lockentry, register __a0 struct Hook *hook)
  53. {
  54.    if (lockentry) {
  55.       *array++ = lockentry->lock_address;
  56.       *array++ = lockentry->lock_access;
  57.       *array++ = lockentry->lock_path;
  58.       *array   = NULL;
  59.    } else {
  60.       *array++ = ESC "bAddress";
  61.       *array++ = ESC "bAccess";
  62.       *array++ = ESC "bPath";
  63.       *array   = NULL;
  64.    }
  65.    return(0);
  66. }
  67.  
  68. struct Hook locklist_dsphook = {
  69.  {NULL, NULL},
  70.  (ULONG (* )())locklist_dspfunc,
  71.  NULL, NULL
  72. };
  73.  
  74. void FreeLocks (void)
  75. {
  76.     MyFreePoolStructs(&LockPool, locktext, locktext2, locklist);
  77. }
  78.  
  79. APTR GetLocks (int sl_job, char *pattern, struct LockEntry **first) {
  80.    struct   LockEntry      *lockentry,*previous = NULL;
  81.    struct   DosList        *dev;
  82.    unsigned long           lock, next, lock2remove = 0;
  83.    struct   FileLock       *lockptr;
  84.    struct   FileInfoBlock  *infoBlock;
  85.    APTR     LocalPool;
  86.  
  87.    unsigned char  *dest;
  88.    unsigned char  access[32];
  89.  
  90.    int   erster, pattern_correct, i = LOCKSUPDATERATE;
  91.  
  92.    if (first)
  93.       *first = 0;
  94.  
  95.    lockanzahl = 0;
  96.    hiddenanzahl = 0;
  97.    NoReqOn();
  98.  
  99.    LocalPool = tbCreatePool(MEMF_CLEAR, 4096, 4096);
  100.  
  101.    if (dest = tbAllocVecPooled(LocalPool, PATHLENGTH * 2 + 3)) {
  102.       pattern_correct = ((((!pattern) || (*pattern == '\0')) && \
  103.          (ParsePatternNoCase ("#?", dest, PATHLENGTH*2+2) != -1)) || \
  104.          (ParsePatternNoCase (pattern, dest, PATHLENGTH*2+2) != -1));
  105.  
  106.       if (clientstate) {
  107.          char  tmpbuffer[256];
  108.    
  109.          if (SendDaemon ("GetLockList")) {
  110.             while ((lockentry = tbAllocPooled(LocalPool, sizeof(struct LockEntry))) \
  111.               && (ReceiveDecodedEntry ((UBYTE *) lockentry, sizeof (struct LockEntry) - 4)) \
  112.               && (sgets (client_socket, tmpbuffer, 256))) {
  113.    
  114.                IsHex (lockentry->lock_address, (long *) &lockentry->lock_ptr);
  115.    
  116.                if (MatchPatternNoCase (dest, tmpbuffer)) {
  117.                   if (lockentry->lock_path = tbAllocPooled(LocalPool, strlen (tmpbuffer) + 1)) {
  118.                      strncpy (lockentry->lock_path, tmpbuffer, strlen (tmpbuffer) + 1);
  119.                   }
  120.                   lockanzahl ++;
  121.    
  122.                   if (! *first)
  123.                      *first = lockentry;
  124.                   if (previous)
  125.                      previous->lock_next = lockentry;
  126.    
  127.                   previous = lockentry;
  128.                } else {
  129.                   hiddenanzahl++;
  130.                }
  131.                if (WI_Locks && !--i) {
  132.                   _sprintf (tmpstr, "%ld shown, %ld hidden...", lockanzahl, hiddenanzahl);
  133.                   i = LOCKSUPDATERATE;
  134.                   DoMethod (AP_Scout,MUIM_Application_InputBuffered);
  135.                   set (locktext2,MUIA_Text_Contents,tmpstr);
  136.                }
  137.             }
  138.          }
  139.       } else {
  140.          if ((pattern_correct) && (infoBlock = AllocDosObject(DOS_FIB, TAG_DONE))) {
  141.             dev = DOSLIST;
  142.             while (dev != 0) {
  143.                if (dev->dol_Type == DLT_VOLUME){
  144.                   b2cstrn(dev->dol_Name, tmpstr, FILENAMELENGTH);
  145.                   strcat (tmpstr, ":");
  146.    
  147.                   if (lock = Lock (tmpstr, SHARED_LOCK)) {
  148.                      erster = TRUE; next = 0;
  149.    
  150.                      while ((next && (next != lock)) || (erster)) {
  151.                         if (erster)
  152.                            next = lock;
  153.                         lockptr = (struct FileLock *) BADDR(next);
  154.                         if (!TypeOfMem(lockptr))
  155.                            break;
  156.                         if (lockptr->fl_Volume != MKBADDR(dev))
  157.                            break;
  158.                         if (Examine (next, infoBlock)) {
  159.                            if (sl_job == SL_LIST) {
  160.                               if (erster) {
  161.                                  strcpy(access, "OWN");
  162.                               } else if (lockptr->fl_Access == SHARED_LOCK) {
  163.                                  strcpy(access, "SHARED");
  164.                               } else if (lockptr->fl_Access == EXCLUSIVE_LOCK) {
  165.                                  strcpy(access, "EXCLUSIVE");
  166.                               } else {
  167.                                  _sprintf(access, "invalid (%ld)", lockptr->fl_Access);
  168.                                  //strcpy(access, "----");
  169.                               }
  170.                            }
  171.                            if (lockptr->fl_Access == EXCLUSIVE_LOCK) {
  172.                               LONG oldAccess;
  173.  
  174.                               Forbid();
  175.  
  176.                               oldAccess = lockptr->fl_Access;
  177.                               lockptr->fl_Access = SHARED_LOCK;
  178.  
  179.                               NameFromLock(next, tmpstr, PATHLENGTH);
  180.  
  181.                               lockptr->fl_Access = oldAccess;
  182.  
  183.                               Permit();
  184.                            } else {
  185.                               NameFromLock (next, tmpstr, PATHLENGTH);
  186.                            }
  187.                         
  188.                            if (MatchPatternNoCase (dest, tmpstr)) {
  189.                               if ((sl_job == SL_LIST) && (lockentry = tbAllocPooled (LocalPool, sizeof(struct LockEntry)))) {
  190.                                  if (! *first)
  191.                                     *first = lockentry;
  192.                                  if (previous)
  193.                                     previous->lock_next = lockentry;
  194.    
  195.                                  lockentry->lock_ptr = (char *) lockptr;
  196.                                  _sprintf (lockentry->lock_address, "$%08lx", lockptr);
  197.                                  strcpy (lockentry->lock_access, access);
  198.    
  199.                                  if (lockentry->lock_path = tbAllocPooled(LocalPool, strlen (tmpstr) + 1)) {
  200.                                     strcpy (lockentry->lock_path, tmpstr);
  201.                                  }
  202.    
  203.                                  lockanzahl ++;
  204.                                  previous = lockentry;
  205.                               } else if (sl_job == SL_REMOVE) {
  206.                                  lockanzahl ++;
  207.                                  lock2remove = next;
  208.                               } else if (sl_job == SL_COUNT) {
  209.                                  lockanzahl ++;
  210.                               } else {
  211.                                  hiddenanzahl++;
  212.                               }
  213.                            } else {
  214.                               hiddenanzahl++;
  215.                            }
  216.                            if ((WI_Locks) && (sl_job == SL_LIST) && (!(--i))) {
  217.                               _sprintf (tmpstr, "%ld shown, %ld hidden...", lockanzahl, hiddenanzahl);
  218.                               i = LOCKSUPDATERATE;
  219.                               DoMethod (AP_Scout,MUIM_Application_InputBuffered);
  220.                               set (locktext2,MUIA_Text_Contents,tmpstr);
  221.                            }
  222.                         }
  223.                         erster = FALSE;
  224.                         next = lockptr->fl_Link;
  225.                         if ((sl_job == SL_REMOVE) && (lock2remove)) {
  226.                            UnLock (lock2remove);
  227.                            lock2remove = (BPTR)NULL;
  228.                         }
  229.                      }
  230.                      UnLock (lock);
  231.                   }
  232.                }
  233.                dev = (struct DosList *) BADDR(dev->dol_Next);
  234.             }
  235.             FreeDosObject(DOS_FIB, infoBlock);
  236.          }
  237.          NoReqOff();
  238.       }
  239.       tbFreeVecPooled(LocalPool, dest);
  240.    }
  241.    return (LocalPool);
  242. }
  243.  
  244. void PrintLocks (char *filename) {
  245.    int   i=1;
  246.    BPTR  handle;
  247.    APTR  lockpool = NULL;
  248.    struct LockEntry *entryp = NULL;
  249.  
  250.    handle = HandlePrintStart (filename);
  251.    if ((handle) && (PrintOneLine (handle, "\n  Address  Access       Path\n\n"))) {
  252.       if (! WI_Locks) {
  253.          lockpool = GetLocks (SL_LIST, "#?", &entryp);
  254.       }
  255.       if (i) {
  256.          for (i=0;;i++) {
  257.             if (WI_Locks)
  258.                DoMethod (locklist,MUIM_List_GetEntry,i,&entryp);
  259.             if (!entryp) break;
  260.  
  261.             _sprintf (tmpstr2, " %s  %-10s       %s\n", entryp->lock_address, entryp->lock_access, entryp->lock_path);
  262.             if (! (PrintOneLine (handle, tmpstr2)))
  263.                break;
  264.  
  265.             if (! WI_Locks)
  266.                entryp = entryp->lock_next;
  267.          }
  268.       }
  269.    }
  270.    if (lockpool) tbDeletePool(lockpool);
  271.    HandlePrintStop();
  272. }
  273.  
  274. void ShowLocks (void) {
  275.    unsigned char        *pattern;
  276.    struct   LockEntry   *lock;
  277.  
  278.    ApplicationSleep();
  279.    set (locklist,MUIA_List_Quiet,TRUE);
  280.    set (locklist,MUIA_List_CompareHook,locklist_cmphook_ptr);
  281.    set (BT_LockRemove, MUIA_Disabled, TRUE);
  282.  
  283.    FreeLocks();
  284.  
  285.    get (lockpattern, MUIA_String_Contents, &pattern);
  286.    LockPool = GetLocks (SL_LIST, pattern, &lock);
  287.  
  288.    while (lock) {
  289.       InsertSortedEntry (locklist, (APTR *) &lock);
  290.       lock = lock->lock_next;
  291.    }
  292.  
  293.    AwakeApplication();
  294.    set (locklist, MUIA_List_Quiet, FALSE);
  295.    MySetContents (locktext2, "%ld shown, %ld hidden. Done.", lockanzahl, hiddenanzahl);
  296.    set (WI_Locks,MUIA_Window_ActiveObject,locklist);
  297. }
  298.  
  299. void SendLockList (void) {
  300.    unsigned char        *pattern;
  301.    struct   LockEntry   *lock;
  302.  
  303.    FreeLocks();
  304.  
  305.    get (lockpattern, MUIA_String_Contents, &pattern);
  306.    LockPool = GetLocks (SL_LIST, pattern, &lock);
  307.  
  308.    while (lock) {
  309.       SendEncodedEntry ((UBYTE *) lock, sizeof (struct LockEntry) - 4);
  310.       if (lock->lock_path && lock->lock_path[0])
  311.          SendClient ((UBYTE *) lock->lock_path);
  312.       else
  313.          SendClient (" ");
  314.       lock = lock->lock_next;
  315.    }
  316.    FreeLocks();
  317. }
  318.  
  319.  
  320. APTR WI_Locks, locklist, locktext, lockcount, CY_LockSort;
  321. int  locksortstate = 0;
  322. APTR lockpattern,locktext2;
  323. APTR BT_LockUpdate, BT_LockPrint, BT_LockRemove, BT_LockExit;
  324.  
  325. APTR LockSortList[] = {
  326.    &list_cmpnormalhook,
  327.    &locklist_cmppathhook,
  328.    &list_cmpaddresshook,
  329.    NULL
  330. };
  331.  
  332. static const char *CYA_LockSortText[] = {
  333.    "normal",
  334.    "path",
  335.    "address",
  336.    NULL
  337. };
  338.  
  339. char locks_title[WINDOWTITLELEN];
  340.  
  341. void LocksWindow (BOOL state) {
  342.    if (state) {
  343.       if (WI_Locks) {
  344.          ShowLocks();
  345.       } else {
  346.          WI_Locks = WindowObject,
  347.          MUIA_Window_Title, MyGetWindowTitle (locks_title, "LOCKS"),
  348.          MUIA_HelpNode, LocksText,
  349.          MUIA_Window_ID, MakeListID('L','O','C','K'),
  350.          WindowContents, VGroup,
  351.             Child, locklist = ListviewObject,
  352.                MUIA_Listview_MultiSelect, TRUE,
  353.                MUIA_Listview_List, ListObject,
  354.                MUIA_List_Format, "COL=0 DELTA=8,COL=1 DELTA=8 P=\33c,COL=2",
  355.                MUIA_List_Title, TRUE,
  356.                MUIA_List_DisplayHook, &locklist_dsphook,
  357.                InputListFrame,
  358.                End,
  359.             MUIA_CycleChain, TRUE,
  360.             End,
  361.             Child, HGroup,
  362.                Child, locktext = MyTextObject5(SPACE40),
  363.                Child, HGroup, MUIA_HorizWeight, 0,
  364.                   Child, MyLabel ("sorted by:"),
  365.                   Child, CY_LockSort = CycleObject,
  366.                      MUIA_Cycle_Entries, CYA_LockSortText,
  367.                      MUIA_Cycle_Active, locksortstate,
  368.                      MUIA_HorizWeight, 0,
  369.                   End,
  370.                End,
  371.             End,
  372.             Child, locktext2 = MyTextObject(),
  373.             Child, HGroup,
  374.                Child, KeyLabel("Pattern:", 'p'),
  375.                Child, lockpattern = StringObject,
  376.                   MUIA_String_Contents, "#?", MUIA_ControlChar, 'p',
  377.                   MUIA_String_MaxLen, TEXTLENGTH, StringFrame,
  378.                End,
  379.             End,
  380.             Child, MyVSpace(2),
  381.             Child, HGroup, MUIA_Group_SameSize, TRUE,
  382.                Child, BT_LockUpdate   = KeyButtonA (UpdateText,ID_LOCKUPDATE),
  383.                Child, BT_LockPrint    = KeyButtonA (PrintText ,ID_LOCKPRINT),
  384.                Child, BT_LockRemove   = KeyButtonA (RemoveText,ID_LOCKREMOVE),
  385.                Child, BT_LockExit     = KeyButtonA (ExitText  ,ID_LOCKEXIT),
  386.             End,
  387.          End, End;
  388.  
  389.          DoMethod (AP_Scout,OM_ADDMEMBER,WI_Locks);
  390.          DoMethod (WI_Locks,MUIM_Window_SetCycleChain,locklist,CY_LockSort,lockpattern,BT_LockUpdate,BT_LockPrint,BT_LockRemove,BT_LockExit,NULL);
  391.          DoMethod (CY_LockSort, MUIM_Notify, MUIA_Cycle_Active, MUIV_EveryTime, AP_Scout, 2, MUIM_Application_ReturnID, ID_LOCKSORT);
  392.          DoMethod (lockpattern,MUIM_Notify,MUIA_String_Acknowledge,MUIV_EveryTime,AP_Scout,2,MUIM_Application_ReturnID,ID_LOCKUPDATE);
  393.  
  394.          SetCloseRequest (WI_Locks,ID_LOCKEXIT);
  395.          SetListActive (locklist,ID_LOCKLV_ACTIVE);
  396.  
  397.          SetWindowOpen (WI_Locks,lockpattern,ID_LOCKEXIT);
  398.       }
  399.    } else if ((! state) && (WI_Locks)) {
  400.       SetWindowClose (WI_Locks,TRUE);
  401.  
  402.       FreeLocks();
  403.  
  404.       DoMethod (AP_Scout,OM_REMMEMBER,WI_Locks);
  405.       MUI_DisposeObject (WI_Locks);
  406.       WI_Locks = NULL;
  407.       locklist = NULL;
  408.    }
  409. }
  410.  
  411.